/*
 * @Author: your name
 * @Date: 2024-04-17 18:28:08
 * @LastEditTime: 2024-04-17 18:38:10
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \.leetcode\199.二叉树的右视图.cpp
 */
/*
 * @lc app=leetcode.cn id=199 lang=cpp
 *
 * [199] 二叉树的右视图
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> rightSideView(TreeNode* root) {
        vector<int> res;
        if(root == nullptr){
            return res;
        }
        queue<TreeNode* > que;
        que.push(root);
        while(!que.empty()){
            TreeNode* tmp_node = que.front();
            int size = que.size();
            for(int i = 0; i < size; i++){
                TreeNode* tmp_node = que.front();// 获取队头
                if(i==size-1)res.push_back(tmp_node->val);
                que.pop();
                if(tmp_node->left != nullptr)  que.push(tmp_node->left);
                if(tmp_node->right != nullptr)  que.push(tmp_node->right);

            }
        }
        return res;
    }
};
// @lc code=end

